import Vue from 'vue';
import 'babel-polyfill';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import './assets/icon/iconfont.css'; // 加载ant design的icon样式
import App from './App';
import router from './router';
import components from './components/'; // 加载公共组件
import localComponents from './localComponents/'; // 加载本地公共组件
import * as filters from './utils/filters'; // 全局vue filter
import utils from './utils'; // 全局vue utils
import localJs from './localJs';
import common from './utils/common'; // 全局vue ep
import * as api from './api/index';
import store from './vuex';
import canvas from './utils/html2canvas.min';
import rules from './utils/rules';
import localRules from './localJs/rules';
import promise from 'es6-promise';

promise.polyfill();

Vue.use(ElementUI);

Vue.config.productionTip = false;

//注册过滤器
Object.keys(filters).forEach(key => {
    Vue.filter(key, filters[key]);
});

// 公共组件首字母大写
Object.keys(components).forEach((key) => {
    var name = key.replace(/(\w)/, (v) => v.toUpperCase());
    Vue.component(`ep${name}`, components[key]);
});

// 本地公共组件首字母大写
Object.keys(localComponents).forEach((key) => {
    var name = key.replace(/(\w)/, (v) => v.toUpperCase());
    Vue.component(`epl${name}`, localComponents[key]);
});

/**
 * 弹出框
 * @param {*} title
 * @param {*} text
 * @param {*} confirmButtonText
 */
let alert = function (Objcet) {
    if (typeof Objcet == 'object') {
        let box = document.createElement('div');
        let titleBox = document.createElement('div');
        let bodyTitle = document.createElement('div');
        let bodyContent = document.createElement('div');
        let body = document.createElement('div');
        let foot = document.createElement('div');
        let confirm = document.createElement('button');
        let mask = document.createElement('div');
        let title = Objcet.title || '提示';
        if (Objcet.textType == 'Array') {
            let text = '';
            Objcet.text.map(item => {
                if(Objcet.type == 'success'){
                    text += "<p><i style='font-size: 16px;' class='el-icon-circle-check-outline epl-icon-success'></i>" + item + "</p>";
                }else if(Objcet.type == 'error'){
                    text += "<p><i style='font-size: 16px;' class='el-icon-circle-close-outline epl-icon-error'></i>" + item + "</p>";
                }else{
                    text += "<p>" + item + "</p>";
                }
            })
            bodyContent.innerHTML = text || '';
        }
        else {
            bodyContent.innerHTML = Objcet.text || '';
        }
        bodyTitle.innerHTML = Objcet.titleBody || '';
        confirm.innerHTML = Objcet.confirmButtonText || '确定';
        box.className = 'ep-messgebox';
        bodyTitle.className = 'ep-messge-body-title';
        bodyContent.className = 'ep-messge-body-content';
        titleBox.className = 'ep-messgebox-hd';
        if (Objcet.type == 'success') {
            titleBox.innerHTML = "<i class='el-icon-success epl-icon-success'></i><span>" + title + '</span>';
        } else if (Objcet.type == 'error') {
            titleBox.innerHTML = "<i class='el-icon-error epl-icon-error'></i><span>" + title + '</span>';
        } else if (Objcet.type == 'warning') {
            titleBox.innerHTML = "<i class='el-icon-warning epl-icon-warning'></i><span>" + title + '</span>';
        } else {
            titleBox.innerHTML = "<span>" + title + '</span>';
        }
        body.className = 'ep-messgebox-bd ep-border-bottom';
        foot.className = 'ep-messgebox-fd';
        confirm.className = 'ep-messgebox-btn ep-messgebox-confirm';
        mask.className = 'ep-mask';
        document.body.appendChild(box);
        document.body.appendChild(mask);
        body.appendChild(bodyTitle);
        body.appendChild(bodyContent);
        box.appendChild(titleBox);
        box.appendChild(body);
        box.appendChild(foot);
        foot.appendChild(confirm);
        return new Promise((resolve, reject) => {
            confirm.onclick = function () {
                document.body.removeChild(box);
                document.body.removeChild(mask);
                resolve(true);
            };
        });
    } else {
        console.error("请使用Objcet格式:{title: '提示',text: '这里是提示内容', confirmButtonText: '确定'}");
    }
};

/**
 * 显示Loading
 */
let showLoading = function () {
    let box = document.createElement('div');
    let loading = document.createElement('div');
    let mask = document.createElement('div');
    let div = document.createElement('div');
    box.className = 'ep-loading';
    loading.className = 'ep-dual-ring';
    mask.className = 'ep-loading-mask';
    document.body.appendChild(box);
    document.body.appendChild(mask);
    box.appendChild(loading);
    loading.appendChild(div);
};

/**
 * 隐藏Loading
 */
let hideLoading = function () {
    let box = document.querySelector('.ep-loading');
    let mask = document.querySelector('.ep-loading-mask');
    if (box && mask) {
        document.body.removeChild(box);
        document.body.removeChild(mask);
    }
};

// 全局注册
Vue.prototype.$api = api;
Vue.prototype.utils = utils;
Vue.prototype.common = common;
Vue.prototype.canvas = canvas;
Vue.prototype.localJs = localJs;
Vue.prototype.$rules = rules;
Vue.prototype.$localRules = localRules;
Vue.prototype.$alert = alert;
Vue.prototype.$showLoading = showLoading;
Vue.prototype.$hideLoading = hideLoading;

// 解决嵌入原生APP标题问题
router.beforeEach((to, from, next) => {
    Vue.prototype.route = to;
    store.dispatch('SET_PATH', to.path);
    let ticket = to.query.ticket;
    let sessionTicket = sessionStorage.getItem('ticket');
    let params = {
        ticket: ticket
    };
    if (ticket !== undefined && ticket !== sessionTicket) {
        api.commonPost('sso/loginApp', params).then(res => {
            if (res.status === '0') {
                sessionStorage.setItem('ticket', res.data.ticket);
            }
        });
    }
    /* 路由发生变化修改页面meta */
    if (to.meta.content) {
        let head = document.getElementsByTagName('head');
        let meta = document.createElement('meta');
        meta.content = to.meta.content;
        head[0].appendChild(meta)
    }
    /* 路由发生变化修改页面title */
    if (to.meta.title) {
        document.title = to.meta.title;
    }
    next()
});

window.vueInstance = new Vue({
    el: '#app',
    router,
    store,
    components: {App},
    template: '<App/>'
});
